{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray与Python原生list运算效率对比",
   "id": "5328fd05a244a0bb"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-04T12:41:45.774616Z",
     "start_time": "2025-03-04T12:40:30.528920Z"
    }
   },
   "source": [
    "import numpy\n",
    "import numpy as np\n",
    "import random\n",
    "import time\n",
    "\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "b = np.array(a)\n",
    "t3 = time.time()\n",
    "sum2 = np.sum(b)\n",
    "t4 = time.time()\n",
    "\n",
    "print(t2 - t1 , t4 - t3)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17.22483730316162 0.5143451690673828\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 创建一维数组",
   "id": "a68576fd8a76fc74"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T12:58:55.006237Z",
     "start_time": "2025-03-04T12:58:54.994100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "list1 = [1,2,3,4]\n",
    "oneArray = np.array(list1)\n",
    "print(type(oneArray))\n",
    "print(oneArray)\n",
    "\n",
    "'''创建数组的多种形式'''\n",
    "#1.直接传入\n",
    "t1 = np.array([1,2,3])\n",
    "print(type(t1))\n",
    "print(t1)\n",
    "\n",
    "#2.传入range生成\n",
    "t2 = np.array(range(10))\n",
    "print(type(t2))\n",
    "print(t2)\n",
    "#3.使用np自带的np.arange()生成\n",
    "t3 = np.arange(0,10,2)\n",
    "print(type(t3))\n",
    "print(t3)\n"
   ],
   "id": "f71bc146d7dd7d54",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n",
      "<class 'numpy.ndarray'>\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 创建二维数组&常用属性",
   "id": "2cc0336f31cb3802"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T13:02:40.154404Z",
     "start_time": "2025-03-04T13:02:40.144395Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "list = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list)\n",
    "\n",
    "#获取数组的维度\n",
    "print(twoArray.ndim)\n",
    "\n",
    "#形状(行,列)\n",
    "print(twoArray.shape)\n",
    "\n",
    "#有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "9e4c3d4abfcc76d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 调整数组的形状",
   "id": "146c6c2baa1ace21"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T13:17:59.534736Z",
     "start_time": "2025-03-04T13:17:59.528461Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([1,2,3,4,5,6,7,8,9,10,11,12])\n",
    "\n",
    "#修改的是原有的\n",
    "print(id(four))\n",
    "four.shape = (2,6)\n",
    "print(four)\n",
    "print(id(four))\n",
    "\n",
    "#返回的是一个新的数组\n",
    "print(id(four))\n",
    "four = four.reshape(3,4)\n",
    "print(four)\n",
    "print(id(four))\n",
    "\n",
    "print('*' * 50)\n",
    "'''\n",
    "将多维数组变为一维数组\n",
    "默认情况下'c'以行为主顺序展开，'F'意味着按列展开\n",
    "'''\n",
    "twelve= four.reshape((6,2),order = 'F')\n",
    "six = four.flatten(order = 'F')\n",
    "print(twelve)\n",
    "print(six)\n",
    "\n",
    "print('*' * 50)\n",
    "\n",
    "#拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "\n",
    "#转换成二维\n",
    "t1 = t.reshape((3,8))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "id": "46162a5bd3ed85f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2234111558256\n",
      "[[ 1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12]]\n",
      "2234111558256\n",
      "2234111558256\n",
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "2236782411824\n",
      "**************************************************\n",
      "[[ 1  3]\n",
      " [ 5  7]\n",
      " [ 9 11]\n",
      " [ 2  4]\n",
      " [ 6  8]\n",
      " [10 12]]\n",
      "[ 1  5  9  2  6 10  3  7 11  4  8 12]\n",
      "**************************************************\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 8  9 10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21 22 23]]\n",
      "(3, 8)\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 将数组转化成list",
   "id": "5ff21933e82e5f4d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T13:28:01.806713Z",
     "start_time": "2025-03-04T13:27:47.523270Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([12,23,34,45,567])\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "88c6c316d257dd77",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 23, 34, 45, 567]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# NumPy的数据类型",
   "id": "e619bca46b86cce"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T14:49:17.179086Z",
     "start_time": "2025-03-04T14:49:17.171133Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "import numpy as np\n",
    "\n",
    "#返回数组中每个元素的字节单位长度，dtype设置数据\n",
    "f = np.array([1,2,3,4,5],dtype = np.int16)\n",
    "print(f.itemsize) #np.int8(一个字节\n",
    "\n",
    "#获取数据类型\n",
    "print(f.dtype)\n",
    "\n",
    "#调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "\n",
    "#拓展：随机生成小数\n",
    "#使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "\n",
    "#取小数点后两位\n",
    "print(np.round(arr,2))"
   ],
   "id": "e9e1e94b844c93fe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "0.57\n",
      "[0.44 0.16 0.55 0.14 0.22 0.2  0.54 0.66 0.73 0.33]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组和数的计算",
   "id": "2a6a75e407e32b84"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T14:51:18.606470Z",
     "start_time": "2025-03-04T14:51:18.602103Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((6,4))\n",
    "print(t1)\n",
    "print(t1 + 2)\n",
    "print(t1 * 2)\n",
    "print(t1 / 2)"
   ],
   "id": "4376e9cc24814648",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组和数之间的计算",
   "id": "c9f5b467159ed9e4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T14:53:20.459106Z",
     "start_time": "2025-03-04T14:53:20.453313Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(100,124).reshape((6,4))\n",
    "\n",
    "print(t1 + t2)\n",
    "print('*' * 50)\n",
    "print(t1 * t2)\n",
    "print('*' * 50)"
   ],
   "id": "eabc24fdebe8e2b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "**************************************************\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T14:55:56.568040Z",
     "start_time": "2025-03-04T14:55:56.551081Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#不同形状的多重数组间的计算\n",
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(12).reshape((3,4))\n",
    "print(t1 + t2)\n",
    "print(t1 * t2)"
   ],
   "id": "82b4c6d63f737b13",
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (6,4) (3,4) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[38], line 4\u001B[0m\n\u001B[0;32m      2\u001B[0m t1 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m24\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape((\u001B[38;5;241m6\u001B[39m,\u001B[38;5;241m4\u001B[39m))\n\u001B[0;32m      3\u001B[0m t2 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m12\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape((\u001B[38;5;241m3\u001B[39m,\u001B[38;5;241m4\u001B[39m))\n\u001B[1;32m----> 4\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mt2\u001B[49m)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1 \u001B[38;5;241m*\u001B[39m t2)\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (6,4) (3,4) "
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T15:00:12.754219Z",
     "start_time": "2025-03-04T15:00:12.750028Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#行数或列数的一维数组和多维数组可以进行计算\n",
    "t1 = np.arange(24).reshape((3,8))\n",
    "t2 = np.arange(8)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 + t2)\n",
    "print(t1 * t2)"
   ],
   "id": "494384a32ccb8f99",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 8  9 10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21 22 23]]\n",
      "[0 1 2 3 4 5 6 7]\n",
      "[[ 0  2  4  6  8 10 12 14]\n",
      " [ 8 10 12 14 16 18 20 22]\n",
      " [16 18 20 22 24 26 28 30]]\n",
      "[[  0   1   4   9  16  25  36  49]\n",
      " [  0   9  20  33  48  65  84 105]\n",
      " [  0  17  36  57  80 105 132 161]]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T15:02:01.198932Z",
     "start_time": "2025-03-04T15:02:01.193127Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((3,8))\n",
    "t2 = np.arange(8)\n",
    "t3 = np.arange(3).reshape((3,1))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t3)\n",
    "print(t1 + t2)\n",
    "print(t1 * t2)\n",
    "print(t1 + t3)\n",
    "print(t1 * t3)"
   ],
   "id": "8d6de875f60b280a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 8  9 10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21 22 23]]\n",
      "[0 1 2 3 4 5 6 7]\n",
      "[[0]\n",
      " [1]\n",
      " [2]]\n",
      "[[ 0  2  4  6  8 10 12 14]\n",
      " [ 8 10 12 14 16 18 20 22]\n",
      " [16 18 20 22 24 26 28 30]]\n",
      "[[  0   1   4   9  16  25  36  49]\n",
      " [  0   9  20  33  48  65  84 105]\n",
      " [  0  17  36  57  80 105 132 161]]\n",
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 9 10 11 12 13 14 15 16]\n",
      " [18 19 20 21 22 23 24 25]]\n",
      "[[ 0  0  0  0  0  0  0  0]\n",
      " [ 8  9 10 11 12 13 14 15]\n",
      " [32 34 36 38 40 42 44 46]]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中的轴",
   "id": "b4f6ba80f3908dfc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T15:14:34.993728Z",
     "start_time": "2025-03-04T15:14:34.962590Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.arange(8).reshape(2,2,2)\n",
    "print(a)\n",
    "print('-' * 50)\n",
    "b = np.sum(a,axis = 0)\n",
    "print(b)\n",
    "print('-' * 50)\n",
    "c = np.sum(a,axis = 1)\n",
    "print(c)\n",
    "print('-' * 50)\n",
    "d = np.sum(a,axis = 2)\n",
    "print(d)\n",
    "print('-' * 50)"
   ],
   "id": "4a09c1d62dc08371",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]]\n",
      "--------------------------------------------------\n",
      "[[ 4  6]\n",
      " [ 8 10]]\n",
      "--------------------------------------------------\n",
      "[[ 2  4]\n",
      " [10 12]]\n",
      "--------------------------------------------------\n",
      "[[ 1  5]\n",
      " [ 9 13]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 多维数组的操作",
   "id": "6e017e78159c7b5b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:02:44.624249Z",
     "start_time": "2025-03-04T16:02:44.617011Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*' * 50)\n",
    "print(t1[1]) #取一行\n",
    "print('*' * 50)\n",
    "print(t1[1:]) #取多行\n",
    "print('*' * 50)\n",
    "print(t1[1:3,:])#取连续的多行\n",
    "print('*' * 50)\n",
    "print(t1[[0,2,3]]) #取不连续的多行\n",
    "print('*' * 50)\n",
    "print(t1[[0,2,3],:]) #取不连续的多行\n",
    "print('*' * 50)\n",
    "print(t1[:,1]) #取一列\n",
    "print('*' * 50)\n",
    "print(t1[:,1:]) #取连续的多列\n",
    "print('*' * 50)\n",
    "print(t1[:,[0,2,3]]) #取不连续的多列\n",
    "print('*' * 50)\n",
    "print(t1[2,3]) #取一个值\n",
    "print('*' * 50)"
   ],
   "id": "7b0d600f186869e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "**************************************************\n",
      "[ 6  7  8  9 10 11]\n",
      "**************************************************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "**************************************************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "**************************************************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "**************************************************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "**************************************************\n",
      "[ 1  7 13 19]\n",
      "**************************************************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "**************************************************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "**************************************************\n",
      "15\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中的数值修改",
   "id": "d36febbf8bdc4063"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:06:06.283221Z",
     "start_time": "2025-03-04T16:06:06.278488Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "t = np.arange(24).reshape((4,6))\n",
    "print(t)"
   ],
   "id": "c738aaa5ea831232",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:06:35.959142Z",
     "start_time": "2025-03-04T16:06:35.954340Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#修改某一列的值\n",
    "t[:,1] = 0\n",
    "print(t)"
   ],
   "id": "bdd74ec0fff5c5df",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  2  3  4  5]\n",
      " [ 6  0  8  9 10 11]\n",
      " [12  0 14 15 16 17]\n",
      " [18  0 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:07:18.847713Z",
     "start_time": "2025-03-04T16:07:18.843930Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#修改某一行的值\n",
    "t[1,:] = 0\n",
    "print(t)"
   ],
   "id": "387d9f8372af9d5c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [12  0 14 15 16 17]\n",
      " [18  0 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:08:01.311330Z",
     "start_time": "2025-03-04T16:08:01.307586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#修改连续多行\n",
    "t[1:3,:] = 1\n",
    "print(t)"
   ],
   "id": "933d69e72dcc90bb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  2  3  4  5]\n",
      " [ 1  1  1  1  1  1]\n",
      " [ 1  1  1  1  1  1]\n",
      " [18  0 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:08:34.512180Z",
     "start_time": "2025-03-04T16:08:34.508171Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#修改连续多列\n",
    "t[:,1:3] = 123\n",
    "print(t)"
   ],
   "id": "129c28817a15ba35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0 123 123   3   4   5]\n",
      " [  1 123 123   1   1   1]\n",
      " [  1 123 123   1   1   1]\n",
      " [ 18 123 123  21  22  23]]\n"
     ]
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:09:15.550823Z",
     "start_time": "2025-03-04T16:09:15.546637Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#修改多行多列\n",
    "t[1:3,2:4] = 321\n",
    "print(t)"
   ],
   "id": "f613da79c2983db3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0 123 123   3   4   5]\n",
      " [  1 123 321 321   1   1]\n",
      " [  1 123 321 321   1   1]\n",
      " [ 18 123 123  21  22  23]]\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:11:23.571623Z",
     "start_time": "2025-03-04T16:11:23.567941Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#修改不连续的点\n",
    "t[[0,0],[0,3]] = 666\n",
    "print(t)"
   ],
   "id": "ef4482901df971ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[666 123 123 666   4   5]\n",
      " [  1 123 321 321   1   1]\n",
      " [  1 123 321 321   1   1]\n",
      " [ 18 123 123  21  22  23]]\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:12:01.219306Z",
     "start_time": "2025-03-04T16:12:01.214375Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#根据条件修改\n",
    "t[t<10] = 0\n",
    "print(t)"
   ],
   "id": "ddc8575d7c8dc41c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[666 123 123 666   0   0]\n",
      " [  0 123 321 321   0   0]\n",
      " [  0 123 321 321   0   0]\n",
      " [ 18 123 123  21  22  23]]\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:13:34.605663Z",
     "start_time": "2025-03-04T16:13:34.601980Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#逻辑判断\n",
    "t[(t>100) | (t < 20)] = 888\n",
    "print(t)"
   ],
   "id": "4657908567b5ee90",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[888 888 888 888 888 888]\n",
      " [888 888 888 888 888 888]\n",
      " [888 888 888 888 888 888]\n",
      " [888 888 888  21  22  23]]\n"
     ]
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T16:14:51.463275Z",
     "start_time": "2025-03-04T16:14:51.459322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t[(t<23) & (t > 21)] = 66\n",
    "print(t)"
   ],
   "id": "2f2b379d7033daee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[888 888 888 888 888 888]\n",
      " [888 888 888 888 888 888]\n",
      " [888 888 888 888 888 888]\n",
      " [888 888 888  21  66  23]]\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的添加",
   "id": "a22653e166beaa24"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T03:06:10.183314Z",
     "start_time": "2025-03-05T03:06:10.177878Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "\n",
    "print(np.append(a,[7,8,9]))\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "\n",
    "b = np.append(a,[7,8,9])\n",
    "print(b)\n",
    "# print(id(a))\n",
    "# print(id(b))\n",
    "print('*' * 50)\n",
    "\n",
    "#沿数轴添加\n",
    "print(np.append(a,[[666],[888]],axis = 1))\n"
   ],
   "id": "543c2535a63a8274",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "**************************************************\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "**************************************************\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "**************************************************\n",
      "[[  1   2   3 666]\n",
      " [  4   5   6 888]]\n",
      "[  1 666 888 999   2   3   4   5   6]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 插入数组",
   "id": "c57474dbd1c0a491"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T03:12:45.949620Z",
     "start_time": "2025-03-05T03:12:45.943509Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2],[3,4],[5,6]])\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"未传递Axis参数，在插入之前输入数组会被展开。\")\n",
    "print(np.insert(a,3,[11,12]))\n",
    "print('\\n')\n",
    "print(\"传递了Axis参数，会广播数组来输入数组。\")\n",
    "print(\"沿着轴 0 广播：\")\n",
    "print(np.insert(a,1,[11],axis = 0))\n",
    "print(np.insert(a,1,[11,12],axis = 0))\n",
    "print(\"沿着轴 1 广播：\")\n",
    "print(np.insert(a,1,[11],axis = 1))\n",
    "print(np.insert(a,1,[666,888,999],axis = 1))"
   ],
   "id": "6c1e01f8b3bc2836",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "**************************************************\n",
      "未传递Axis参数，在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了Axis参数，会广播数组来输入数组。\n",
      "沿着轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "沿着轴 1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n",
      "[[  1 666   2]\n",
      " [  3 888   4]\n",
      " [  5 999   6]]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中的删除",
   "id": "5bab89b0255b89b1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T03:40:01.017162Z",
     "start_time": "2025-03-05T03:40:01.011486Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "print(\"未传递Axis参数，在删除之前输入数组会被展开。\")\n",
    "print(np.delete(a,1))\n",
    "print('\\n')\n",
    "\n",
    "print(\"删除每一行的第二列：\")\n",
    "print(np.delete(a,1,axis = 1))\n"
   ],
   "id": "19b286e9c60bd678",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "**************************************************\n",
      "未传递Axis参数，在删除之前输入数组会被展开。\n",
      "[ 0  2  3  4  5  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "删除每一行的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组去重",
   "id": "b8ad289dde55c19f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T07:03:44.781168Z",
     "start_time": "2025-03-05T07:03:44.770657Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "\n",
    "print(\"第一个数组：\")\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"第一个数组的去重值：\")\n",
    "u = np.unique(a)\n",
    "print(u)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"去重数组的索引数组：\")\n",
    "u,indices = np.unique(a, return_index=True)\n",
    "print(u)\n",
    "print(indices)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"我们可以看到每个和原数组下标对应的数值：\")\n",
    "print(a)\n",
    "print(\"去重数组的下标：\")\n",
    "u,indices = np.unique(a,return_inverse=True)\n",
    "print(u)\n",
    "print(indices)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"返回去重元素的重复数量：\")\n",
    "u,indices = np.unique(a, return_counts=True)\n",
    "print(u)\n",
    "print(indices)"
   ],
   "id": "635be35ad53a8081",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "**************************************************\n",
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n",
      "**************************************************\n",
      "去重数组的索引数组：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 4 7 9]\n",
      "**************************************************\n",
      "我们可以看到每个和原数组下标对应的数值：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "**************************************************\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的计算（数学函数",
   "id": "897751728efdfc1a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T07:34:23.882343Z",
     "start_time": "2025-03-05T07:34:23.873231Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "#获取所有数据的最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "#获取某一个轴上的最大值\n",
    "result = np.max(score,axis=0)\n",
    "print(result)\n",
    "#获取某一个轴上的最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n",
    "\n",
    "#数据的比较\n",
    "result = np.maximum([-2,-1,0,1,2],0) #第一个参数中的每个数与第二个参数比较并返回大的\n",
    "print(result)\n",
    "result = np.minimum([-2,-1,0,1,2],0) #第一个参数中的每个数与第二个参数比较并返回小的\n",
    "print(result)\n",
    "result = np.maximum([-2,-1,0,1,2],[2,1,0,-1,-2]) #参数格式大小可相等\n",
    "print(result)\n",
    "\n",
    "#求平均值\n",
    "result = np.mean(score) #所有数据的平均值\n",
    "print(result) \n",
    "result = np.mean(score,axis=0) #某行或某列的平均值\n",
    "print(result)\n",
    "\n",
    "#求前缀和 (累加前面的行或列\n",
    "arr = np.arange(1,7).reshape(2,3)\n",
    "print(arr)\n",
    "print('*' * 50)\n",
    "print(arr.cumsum(axis = 0))\n",
    "print(arr.cumsum(1))\n",
    "print('*' * 50)\n",
    "\n",
    "#argmin求最小值索引\n",
    "print(score)\n",
    "result = np.argmin(score,axis = 0) #索引\n",
    "res = np.min(score,axis = 0) #值\n",
    "print(result,res)\n",
    "print('*' * 50)\n",
    "\n",
    "'''求标准差'''\n",
    "result = np.std(score,axis = 0)\n",
    "print(result)\n",
    "\n",
    "'''求极值'''\n",
    "result = np.ptp(score,axis = 0)\n",
    "print(result)\n",
    "\n",
    "'''\n",
    "方差：var\n",
    "协方差：cov\n",
    "计算平均值：average\n",
    "计算中位数：median\n",
    "'''"
   ],
   "id": "2b0e40750ac6784f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[82 88]\n",
      "[75 81]\n",
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[2 1 0 1 2]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "**************************************************\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n",
      "**************************************************\n",
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[2 1] [75 81]\n",
      "**************************************************\n",
      "[2.94392029 3.29983165]\n",
      "[7 7]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的拼接",
   "id": "b058a878d777f360"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T07:56:04.454719Z",
     "start_time": "2025-03-05T07:56:04.448041Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print('*' * 50)\n",
    "#连接两个数组\n",
    "print(np.concatenate((a,b),axis = 0))\n",
    "print(np.concatenate((b,a),axis = 1))\n",
    "print('*' * 50)\n",
    "\n",
    "#根据轴进行堆叠\n",
    "print(np.stack((a,b),axis = 0))\n",
    "print(np.stack((b,a),axis = 1))\n",
    "print('*' * 50)\n",
    "\n",
    "#矩阵垂直拼接\n",
    "v1 = [[0,1,2,3,4,5],[6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],[18,19,20,21,22,23]]\n",
    "result = np.vstack((v2,v1))\n",
    "print(result)\n",
    "\n",
    "#矩阵水平拼接\n",
    "result = np.hstack((v1,v2))\n",
    "print(result)"
   ],
   "id": "a1fb9650dd68716d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]]\n",
      "[[5 6]\n",
      " [7 8]]\n",
      "**************************************************\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "[[5 6 1 2]\n",
      " [7 8 3 4]]\n",
      "**************************************************\n",
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "[[[5 6]\n",
      "  [1 2]]\n",
      "\n",
      " [[7 8]\n",
      "  [3 4]]]\n",
      "**************************************************\n",
      "[[12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]\n",
      " [ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]]\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的分割",
   "id": "99de0397ebf0b34d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T07:59:36.617486Z",
     "start_time": "2025-03-05T07:59:36.612586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.arange(16).reshape(4,4)\n",
    "\n",
    "print(\"第一个数组：\")\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"竖直分割：\")\n",
    "b = np.vsplit(a,2)\n",
    "print(b)\n",
    "\n",
    "print(\"水平分割：\")\n",
    "b = np.hsplit(a,2)\n",
    "print(b)"
   ],
   "id": "12df799d4150b6a6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "**************************************************\n",
      "竖直分割：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n",
      "水平分割：\n",
      "[array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中的nan和inf",
   "id": "88b864265f13805e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T08:16:16.189732Z",
     "start_time": "2025-03-05T08:16:16.183338Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "inf:最大整数据       nan:缺失的数据\n",
    "'''\n",
    "\n",
    "import numpy as np\n",
    "#创建一个nan和inf\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))\n",
    "print('*' * 50)\n",
    "\n",
    "#判断数组中nan的个数\n",
    "t = np.arange(24,dtype = float).reshape(4,6)\n",
    "print(t)\n",
    "#将三行四列数据设为nan\n",
    "t[3,4] = np.nan\n",
    "print(t)\n",
    "#判断非0的个数\n",
    "print(np.count_nonzero(t))\n",
    "#判断nan的个数（利用nan != nan的结果是TRUE\n",
    "print(np.count_nonzero(t != t))\n",
    "print('*' * 50)\n",
    "\n",
    "#将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "f24c2bf4b06a1389",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n",
      "**************************************************\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "23\n",
      "1\n",
      "**************************************************\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T08:17:28.398809Z",
     "start_time": "2025-03-05T08:17:28.394865Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan == np.nan",
   "id": "8ee5f404443d2780",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T08:17:40.666629Z",
     "start_time": "2025-03-05T08:17:40.662570Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan != np.nan",
   "id": "1481950459784a96",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T08:18:12.144185Z",
     "start_time": "2025-03-05T08:18:12.140866Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.nan\n",
    "b = 100\n",
    "c = a * b\n",
    "print(c)"
   ],
   "id": "45b7e4ba0c331bc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 二维数组的转置，轴滚动",
   "id": "ad2702889ee79834"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-05T08:32:58.451284Z",
     "start_time": "2025-03-05T08:32:58.445419Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print(\"原数组：\")\n",
    "print(a)\n",
    "print('*' * 50)\n",
    "\n",
    "print(\"对换数组：\")\n",
    "print(np.transpose(a))\n",
    "\n",
    "#与transpose一致\n",
    "print(a.T)\n",
    "print('*' * 50)\n",
    "\n",
    "#函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0)\n",
    "print(\"原数组：\")\n",
    "print(t1)\n",
    "print(\"调用swapaxes函数后的数组：\")\n",
    "print(re)"
   ],
   "id": "48f3b6451e1e5ba2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "**************************************************\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "**************************************************\n",
      "原数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "调用swapaxes函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "14cb0121ec3e0ee0"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
